#include <iostream>
#include <unordered_set>

using namespace std;

int SquareSum(int n)
{
    int sum = 0;
    int count = 0;
    int temp = n;
    while (temp != 0)
    {
        count ++;
        temp /= 10;
    }
    for(int i = 0; i < count; i++)
    {
        sum += (n % 10) * (n % 10);
        n /= 10;
    }
    return sum;
}

class Solution 
{
public:
    bool isHappy(int n) 
    {
        unordered_set<int> search_set;
        while(1)
        {
            n = SquareSum(n);
            if(n == 1)
            {
                return true;
            }
            else if(search_set.find(n) == search_set.end())
            {
                search_set.insert(n);
            }
            else
            {
                return false;
            }
        }
    }
};

int main()
{
    Solution sol;
    int test1 = 19;
    int test2 = 2;
    int test3 = 7;
    int test4 = 20;

    cout << "测试用例1: 19 是否为快乐数？" << endl;
    cout << (sol.isHappy(test1) ? "是" : "否") << endl;

    cout << "测试用例2: 2 是否为快乐数？" << endl;
    cout << (sol.isHappy(test2) ? "是" : "否") << endl;

    cout << "测试用例3: 7 是否为快乐数？" << endl;
    cout << (sol.isHappy(test3) ? "是" : "否") << endl;

    cout << "测试用例4: 20 是否为快乐数？" << endl;
    cout << (sol.isHappy(test4) ? "是" : "否") << endl;

    return 0;
}

